﻿#define CONN
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Configuration;
using Teradata.Client.Provider;
using System.IO;


public partial class Home : System.Web.UI.Page
{

    bool bWorkingDisconnected = Convert.ToBoolean(ConfigurationManager.AppSettings["WorkingDisconnected"]);

    protected void Page_Load(object sender, EventArgs e)
    {
        //This application's pool gets recycled at 8:00, 9:00, 11:00 and 14:00 
        if (!IsPostBack)
            if ((Boolean)HttpContext.Current.Application["Reload Cache"] == true)
            {
               DataMaintWebService.RebuildTheCache();
               DataMaintWebService.ResetCacheDate();
            }
        Caching.CachingText = "Finis";
    }


    //private  void ResetCacheDate()
    //{
    //    DateTime dtServerSales = DateTime.MinValue;
    //    DateTime dtTeradata = DateTime.MinValue;
    //    dtServerSales = Convert.ToDateTime(System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("SQL\\Data\\ServerSalesDate.txt")));
    //    DateTime dtCachedSales = Convert.ToDateTime(System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("SQL\\Data\\CachedSalesDate.txt")));
    //    System.Web.HttpContext.Current.Application["CacheDate"] = dtCachedSales;

    //    if (!bWorkingDisconnected)
    //    {

    //        bool bFromFile = dtServerSales == dtCachedSales;
    //        if (!bFromFile)
    //        {
    //            System.IO.File.WriteAllText(HttpContext.Current.Server.MapPath("SQL\\Data\\CachedSalesDate.txt"), dtServerSales.ToString());
    //            System.Web.HttpContext.Current.Application["CacheDate"] = dtServerSales;
    //        }
    //    }

    //    HttpContext.Current.Application["Reload Cache"] = false;
    //}


    //public static void RebuildTheCache()
    //{
    //    //We fully reload the cache with these datasets every time the app restarts since they are volatile
    //    Caching oACCCache = new Caching();
    //    oACCCache.Get_DataSet_SQLServer("SCORE_ACC_PROPERTIES", 1, oACCCache.RefreshTheCacheFromSQLServer, false, "");
    //    oACCCache.Get_DataSet_From_SQLServer("ACC_DATA", 1, oACCCache.RefreshTheCache, false, "");

    //    ////This table has ~800k rows, so the initial load needs to pull it in segments, probably 3 months at a time
    //    //oACCCache.Get_DataSet_SQLServer("SCORE_ServiceOrderPullAll", 1, oACCCache.RefreshTheCacheFromSQLServer, false, "BETWEEN DATE-385 AND DATE-270");
    //    ////need to NOT TRUNCATE THE TABLE IN THESE TWO
    //    //oACCCache.Get_DataSet_SQLServer("SCORE_ServiceOrderPullAll", 1, oACCCache.RefreshTheCacheFromSQLServer, false, "BETWEEN DATE-269 AND DATE-150");
    //    //oACCCache.Get_DataSet_SQLServer("SCORE_ServiceOrderPullAll", 1, oACCCache.RefreshTheCacheFromSQLServer, false, "BETWEEN DATE-149 AND DATE-1"); 

    //    oACCCache.Get_DataSet_SQLServer("SCORE_MTD_SALES_ROLLUPS+SCORE_ACC_ORDERS_MTD+SCORE_ACC_ORDERS_PMTD+SCORE_ACC_ORDERS_PMON", 1, oACCCache.RefreshTheCacheFromSQLServer, false, "");

    //    //Fully reloaded to SQL Server each restart

    //    //For these datasets, we only get the current month's data, i.e. an incremental load, since the prior months' totals don't change.
    //    //and they are so large, we don't cache them but select the data we need from SQL Server
    //    //Set the true to false below to load all the data. Also, change the SCORE_ACC_RAW_ORDERS query
    //    oACCCache.Get_DataSet_SQLServer("SCORE_ServiceOrderPullAll", 1, oACCCache.RefreshTheCacheFromSQLServer, true, "BETWEEN DATE-EXTRACT(DAY from (DATE-1)) AND DATE-1");
    //    oACCCache.Get_DataSet_SQLServer("SCORE_STATS_YR_MN_DY+SCORE_ACC_RAW_ORDERS", 1, oACCCache.RefreshTheCacheFromSQLServer, true, "");
    //    oACCCache.Get_DataSet_SQLServer("SCORE_STATS_YR_MN+SCORE_ACC_RAW_ORDERS", 1, oACCCache.RefreshTheCacheFromSQLServer, true, "");
    //    //        oACCCache.Get_DataSet_SQLServer("ACC_CHANNEL_ROLLUPS", 1, oACCCache.RefreshTheCacheFromSQLServer, true, "");


    //    //oACCCache.Get_DataSet("ACC_MARKET_PENETRATION+ACC_PENETRATION", 0, oACCCache.RefreshTheCache, true, "");
    //    //oACCCache.Get_DataSet("MTD_SALES_ROLLUPS", 1, oACCCache.RefreshTheCache, true, "");
    //}



}